var model = require('../model');
var Member = model.Member;

//# 客户信息
exports.index = function(req, res, next) {
    res.render('backend/member/index', {
        title: '客户信息'
    });
};


//# 客户列表
exports.list = function(req, res, next) {
    var pageInfo = {};

    pageInfo.pageIndex = parseInt(req.query.pageIndex) || 1;
    pageInfo.pageSize = parseInt(req.query.pageSize) || 10;
    var skipIndex = (pageInfo.pageIndex - 1) * (pageInfo.pageSize);

    var nameReg = new RegExp(req.query.name);


    Member.count({
        deleted: false,
        name: nameReg
    }, function(error, count) {
        if (error) {
            console.log(error);
        } else {
            pageInfo.count = count;
            pageInfo.totalPage = Math.ceil(count / pageInfo.pageSize);
        }
    });

    Member.find({
            deleted: false,
            name: nameReg
        })
        .skip(skipIndex)
        .limit(pageInfo.pageSize)
        .exec(function(error, members) {
            if (error) {
                console.log(error);
                return;
            }
            res.render('backend/member/list', {
                members: members,
                pageInfo: pageInfo
            });
        });
};

//# 添加客户界面
exports.new = function(req, res, next) {
    res.render('backend/member/new', {
        title: '添加客户'
    });
};

//# 创建客户
exports.create = function(req, res, next) {

    var createMember = {
        name: req.body.name,
        sex: req.body.sex,
        phone: req.body.phone,
        email: req.body.email,
        address: req.body.address,
        birthday: req.body.birthday,
        remark: req.body.remark,
        deleted: false,
    };

    var member = new Member(createMember);

    member.save(function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.append('Location', '/backend/members');
            res.json(result);
        }
    });
};

//# 编辑页面
exports.edit = function(req, res, next) {
    var memberId = req.params.id;

    Member.findOne({
        _id: memberId
    }, function(error, member) {
        if (error) {
            console.log(error);
        } else {
            res.render('backend/member/edit', {
                title: '编辑客户信息',
                member: member
            });
        }
    });
};

//# 更新客户
exports.update = function(req, res, next) {
    
    var memberId = req.params.id;
    
    var updateMember = {
        name: req.body.name,
        sex: req.body.sex,
        phone: req.body.phone,
        email: req.body.email,
        address: req.body.address,
        birthday: req.body.birthday,
        remark: req.body.remark,
        deleted: false,
    };

    Member.update({
        _id: memberId
    }, updateMember, function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};

//# 更新客户
exports.delete = function(req, res, next) {
    var memberId = req.params.id;

    Member.update({
        _id: memberId
    }, {
        deleted: true
    }, function(error) {
        var result = {};
        if (error) {
            result.success = false;
            res.json(result);
        } else {
            result.success = true;
            res.json(result);
        }
    });
};